package com.mypackage.LocationPattern;

import java.util.Calendar;
import java.util.StringTokenizer;

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

public class DatePicker extends Activity  {
	
        public static final String URL = "";
        
        private TextView mDateDisplay;
        private Button mPickDate;
        private Button nextButton;
        private int mYear;
        private int mMonth;
        private int mDay;
        private int wDay;
//        
//        TimePicker timePicker;
//        int hour, minute;
//        static final int TIME_DIALOG_ID = 0;

        static final int DATE_DIALOG_ID = 0;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.datescreen);
        
            //nextButton.setOnClickListener(this);
            // capture our View elements
            mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
            
//            timePicker = (TimePicker) findViewById((Integer) R.id.timePicker);
//            timePicker.setIs24HourView(true);
            
            mPickDate = (Button) findViewById(R.id.pickDate);
            

            // add a click listener to the button
            mPickDate.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
//                	Toast.makeText(getBaseContext(), "Time Selected: "+ "Current Hour: " + timePicker.getCurrentHour() + 
//                			"Current minute: " + timePicker.getCurrentMinute(), Toast.LENGTH_SHORT).show();
                    showDialog(DATE_DIALOG_ID);
                   
                }
            });


            // get the current date
            final Calendar c = Calendar.getInstance();
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);
            
           // wDay = c.get(Calendar.DAY_OF_WEEK);
        
            

            // display the current date (this method is below)
            updateDisplay();
            
            nextButton = (Button) findViewById(R.id.next_button);
            nextButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                	System.out.println("Starting a listener");
                	Intent nextScreen = new Intent(getApplicationContext(), TimePickerHandler.class);
                   // Intent i = new Intent();
                   // i.setClassName("com.mypackage.LocationPattern", "com.mypackage.LocationPattern.TimePickerHandler");
                    startActivity(nextScreen);
                }
            });
     
        }
            
        
        
//        public void onClick(View v){
//        	switch (v.getId()){
//        	case R.id.next_button:
//        		Intent i = new Intent(this, TimePicker.class);
//        		startActivity(i);
//        		break;
//        	
//        	}
//        	switch (v.getId()){
//        	case R.id.pickDate:
////        		Toast.makeText(getBaseContext(), "Time Selected: "+ "Current Hour: " + timePicker.getCurrentHour() + 
////////        			"Current minute: " + timePicker.getCurrentMinute(), Toast.LENGTH_SHORT).show();
//            showDialog(DATE_DIALOG_ID);
//        		break;
//        	
//        	}
//        }
        
        public class Helper extends Calendar{
        	
        	public void recalculateDay ()
        	{
        		//Calendar cal = Calendar.getInstance();
        	 complete();
        	}

			@Override
			public void add(int field, int value) {
				// TODO Auto-generated method stub
				
			}

			@Override
			protected void computeFields() {
				// TODO Auto-generated method stub
				
			}

			@Override
			protected void computeTime() {
				// TODO Auto-generated method stub
				
			}

			@Override
			public int getGreatestMinimum(int field) {
				// TODO Auto-generated method stub
				return 0;
			}

			@Override
			public int getLeastMaximum(int field) {
				// TODO Auto-generated method stub
				return 0;
			}

			@Override
			public int getMaximum(int field) {
				// TODO Auto-generated method stub
				return 0;
			}

			@Override
			public int getMinimum(int field) {
				// TODO Auto-generated method stub
				return 0;
			}

			@Override
			public void roll(int field, boolean increment) {
				// TODO Auto-generated method stub
				
			}
        	
        }
        
        // updates the date in the TextView
        private void updateDisplay() {
        	Calendar cal = Calendar.getInstance();
            //cal.complete();
            mDateDisplay.setText(
                new StringBuilder()
                        // Month is 0 based so add 1
                        .append(mMonth + 1).append("-")
                        .append(mDay).append("-")
                        .append(mYear).append(" "))
                        
                       // .append(wDay).append(" "))
                        ;
        }
        
     // the callback received when the user "sets" the date in the dialog
        private DatePickerDialog.OnDateSetListener mDateSetListener =
                new DatePickerDialog.OnDateSetListener() {
                    @Override
					public void onDateSet(android.widget.DatePicker view,
							int year, int monthOfYear, int dayOfMonth) {
						// TODO Auto-generated method stub
                  Helper h = new Helper();
                  
                        
                    	mYear = year;
                    	mMonth = monthOfYear;
                    	mDay = dayOfMonth;
                    	h.computeTime();
                    	//wDay = h.get(Calendar.DAY_OF_WEEK);
                    	
                    	updateDisplay();
					}
                };
                
                @Override
                protected Dialog onCreateDialog(int id) {
                    switch (id) {
                    case DATE_DIALOG_ID:
                        return new DatePickerDialog(this,
                                    mDateSetListener,
                                    mYear, mMonth, mDay);
                    
//                    case TIME_DIALOG_ID:
//                    	return new TimePickerDialog (this, mTimeSetListener, mHour, mMin, false);
                    }
                    return null;
                }
//                
//                private TimePickerDialog.OnTimeSetListener timeListener = new OnTimeSetListener(){
//                	
//                	
//
//					@Override
//					public void onTimeSet(TimePicker view, int hourOfDay,
//							int minute) {
//						// TODO Auto-generated method stub
//						mHour = hourOfDay;
//                		mMin = minute;
//                	updateDisplay();
//						
//					}
//                };

				
                
                protected int getDayOfWeek(String date){
                	StringTokenizer st = new StringTokenizer(date, "-");
                	Calendar cal = Calendar.getInstance();
                	
                	int month = Integer.parseInt(st.nextToken());
                	int day = Integer.parseInt(st.nextToken());
                	int year = Integer.parseInt(st.nextToken());
                	
                	cal.set(year, month, day);
                	
                	int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
                	return dayOfWeek;
                }
        
    }



